import { MenuItemDefinition } from '@amcax/base';
import { ProductTreeNode } from '../../types';
import { PRODUCT_EVENT } from '@app-cad/common/Constants';
import { userEvents } from '@app-cad/module';
import {
  getAssembleNodesByIds,
  getInvertNode,
} from '@app-base/environments/product/utils';

export const invert: MenuItemDefinition<ProductTreeNode> = {
  id: 'invert',
  children: '反转选择',
  hidden: ({ data, props }) => {
    if (Array.isArray(props)) {
      const nodes = getAssembleNodesByIds(props, data?.docTree);
      return !nodes.some((node) => node.type === 'part');
    }
    return !['part'].includes(props?.type);
  },
  onClick: ({ data, props }) => {
    const nodes = Array.isArray(props)
      ? getInvertNode(props, data?.docTree)
      : getInvertNode(props.key, data?.docTree);
    userEvents.emit(PRODUCT_EVENT.REVERSE_SELECTION, nodes);
  },
};
